package com.henry.Client;

import com.henry.model.Question;
import com.henry.model.User;
import com.henry.model.UserMessage;

import java.io.IOException;
import java.util.List;
import java.util.Scanner;

public class ClientStudent {
    private Scanner sc;
    private ClientInitClose cic;

    public ClientStudent() {
    }

    public ClientStudent(ClientInitClose cic) {
        this.cic = cic;
        sc = ClientScanner.getScanner();
    }

    public void clientStudentLogin() throws IOException, ClassNotFoundException {
        while(true) {
            System.out.println("学员系统");
            System.out.println("请输入账号：");
            String username = sc.next();
            System.out.println("请输入密码：");
            String password = sc.next();
            cic.getOos().writeObject(new UserMessage("studentCheck", new User(username, password)));

            UserMessage userMessage = (UserMessage) cic.getOis().readObject();
            if(userMessage.getType().equals("success")) {
                System.out.println("登录成功！");
                while(true) {
                    System.out.println("[1] 开始考试  [2] 修改密码  [3] 退出");
                    int i = sc.nextInt();
                    switch (i) {
                        case 1:
                            startExam(username);
                            break;
                        case 2:
                            revisePassword(username);
                            break;
                        case 3:
                            return;
                        default:
                            System.out.println("请重新选择！");
                    }
                }
            }else {
                System.out.println("账号或密码错误！");
            }
        }
    }

    private void startExam(String username) throws IOException, ClassNotFoundException {
        ClientDao cd = ClientDao.getCd();
        int grade = 0;
        cic.getOos().writeObject(new UserMessage("getQuestion"));
        UserMessage userMessage = (UserMessage) cic.getOis().readObject();
        List<Question> questions = userMessage.getQuestions();
        for (int i = 0; i < questions.size(); i++) {
            Question q = questions.get(i);
            System.out.println(i + 1 + "." + q.getDescription());
            System.out.println(q.getOptionA() + "  " + q.getOptionB() + "  " + q.getOptionC() + "  " + q.getOptionD());
            System.out.println("请选择：");
            String option = sc.next();
            if(option.equals(q.getAnswer())) {
                grade++;
            }
        }
        cic.getOos().writeObject(new UserMessage("postGrade", new User(username, grade)));
    }
    private void revisePassword(String username) throws IOException {
        while(true) {
            System.out.println("请输入新密码：");
            String fir = sc.next();
            System.out.println("请确认密码：");
            String sec = sc.next();
            if(fir.equals(sec)) {
                cic.getOos().writeObject(new UserMessage("revisePassword", new User(username, sec)));
                System.out.println("修改成功！");
                return;
            }
            System.out.println("两次密码不一致，请重新输入！");
        }
    }
}
